<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" style="margin-bottom:20px">
      <span>状态：</span>
      <el-select v-model="listQuery.type" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <span style="margin-left:10px">ID：</span>
      <el-input @keyup.enter.native="handleFilter" style="width: 160px;" class="filter-item" placeholder="搜索帖子标题ID" v-model="listQuery.id"></el-input>
      <span style="margin-left:10px">标题：</span>
      <el-input @keyup.enter.native="handleFilter" style="width: 160px;" class="filter-item" placeholder="搜索帖子标题" v-model="listQuery.topicTitle">
      </el-input>
      <span style="margin-left:10px">发帖人：</span>
      <el-input @keyup.enter.native="handleFilter" style="width: 160px;" class="filter-item" placeholder="搜索发帖人" v-model="listQuery.topicUser">
      </el-input>
      <span style="margin-left:10px">发帖日期：</span>
      <el-date-picker
        v-model="dateformat"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="yyyy-MM-dd HH:mm:ss"
        :picker-options="pickerOptions">
      </el-date-picker>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="数据加载中，请稍后" border fit highlight-current-row style="width: 100%">  
      <el-table-column min-width="40" align="center" label="ID" prop="id"></el-table-column> 
      <el-table-column min-width="120" align="center" label="发帖人" prop="nickName"></el-table-column>
      <el-table-column min-width="160" align="center" label="发帖标题" prop="title" show-overflow-tooltip></el-table-column>
      <el-table-column min-width="160" align="center" label="发帖内容" prop="content" show-overflow-tooltip></el-table-column>
      <el-table-column min-width="110" align="center" label="发帖时间">
        <template slot-scope="scope">
          <span>{{ scope.row.inTime | dateformat('YYYY-MM-DD HH:mm') }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="60" align="center" label="是否置顶" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.top | statusBoolFilter}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="60" align="center" label="是否精华" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.recommend | statusBoolFilter}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="60" align="center" label="状态" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.showStatus | statusFilter}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="120">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleOprate(scope.row, -1)" v-if="scope.row.showStatus == 1 || scope.row.showStatus == 2">不通过</el-button>
          <el-button size="small" type="primary" @click="handleOprate(scope.row, 1)" v-if="scope.row.showStatus == -1 || scope.row.showStatus == 2">通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.pageNo"
        :page-sizes="[10,20,30,50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getList, toggleId } from '@/api/post'
import { getToken } from '@/utils/auth' // 验权
import { validateURL } from '@/utils/validate'

export default {
  name: 'list',
  data() {
    return {
      options:[{
        value: null,
        label: '全部'
      }, {
        value: '2',
        label: '待审核'
      }, {
        value: '1',
        label: '已通过'
      }, {
        value: '-1',
        label: '不通过'
      }],
      headers:{},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      dateformat:'',
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        'id':null,
        'pageNo': 1,
        'pageSize': 10,
        'topicUser':'',
        'topicTitle':'',
        'type':null
      },
      tableKey: 0
    }
  },
  mounted() {
    this.getList()
    this.headers = { accessToken:getToken() }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1':'通过',
        '-1':'不通过',
        '2':'待审核'
      }
      return statusMap[status]
    },
    statusBoolFilter(status) {
      const statusMap = {
        '0':'否',
        '1':'是'
      }
      return statusMap[status]
    }
  },
  methods: {
    handleOprate(row, status){
      toggleId({ id:row.id, showStatus:status }).then(res => {
        const { code } = res
        if(code == 0){
          this.$notify({
            message: `操作成功`,
            type: 'success',
            duration: 2000
          })
          row.showStatus = status
        }
      })
    },
    getList() {
      this.listLoading = true
      const params = this.listQuery
      const { dateformat } = this
      if(dateformat){
        params['startTime'] = dateformat[0]
        params['endTime'] = dateformat[1]
      }else{
        delete params.startTime
        delete params.endTime
      }
      getList(params).then(response => {
        const { code, data:{ total, list } } = response
        if(code == 0){
          this.total = total
          this.list = list
          this.listLoading = false
        }
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val
      this.getList()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.icon-ctx{
  width:160px;
  height:80px;
  overflow:hidden;
  margin:0 auto;
}
.el-tag + .el-tag {
    margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.app-container{
  padding:20px;
}
.pagination-container{
  margin-top:30px;
}
</style>